package servlet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import model.Goods;
import model.ShopCar;
import model.User;
import service.GoodsService;
import service.ShopCarService;
import service.impl.GoodsServiceImpl;
import service.impl.ShopCarServiceImpl;
import utils.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/shopcar.do")
public class ShopCarServlet extends BaseServlet {
    private ShopCarService shopCarService = new ShopCarServiceImpl();
    private GoodsService goodsService = new GoodsServiceImpl();

    /**
     * 根据商品id和用户id添加购物车记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void addInfo(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        Integer goodsId = Integer.parseInt(req.getParameter("goodsId"));
        Goods goods = goodsService.findById(goodsId);
        HttpSession session = req.getSession();
        boolean flag = false;
        boolean login = true;
        User user = (User) session.getAttribute("user");
        if(user==null){
            login = false;
        }else {
            ShopCar shopCar = new ShopCar(user.getId(), goodsId, 1, goods.getPrice(), goods.getName());
            flag = shopCarService.addInfo(shopCar);
        }
        resp.setContentType("text/plain;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        if(flag){
            out.write("true");
        }else {
            if(login){
                out.write("false");
            }else{
                out.write("nologin");
            }

        }
        out.close();
    }

    /**
     * 根据用户id查找所有购物车记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void findAllInfo(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        List<ShopCar> list = shopCarService.findByUserId(user.getId());

        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if(list!=null){
            String jsonArray = JSONArray.toJSONString(list);
            out.write(jsonArray);
        }else{
            out.write("false");
        }
        out.close();
    }

    /**
     * 根据购物车id删除记录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteInfo(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        boolean flag = shopCarService.delInfoById(id);
        resp.setContentType("text/plain;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if(flag){
            out.write("true");
        }else {
            out.write("false");
        }
        out.close();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp){
          shopCarService.delInfoById(Integer.parseInt(req.getParameter("id")));
    }
}
